package com.ruoyi.stuactivity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.stuactivity.domain.StudentComprehensiveScore;
import com.ruoyi.stuactivity.mapper.StudentComprehensiveScoreMapper;
import com.ruoyi.stuactivity.service.IStudentComprehensiveScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 学生综测成绩 服务层实现
 */
@Service
public class StudentComprehensiveScoreServiceImpl
        extends ServiceImpl<StudentComprehensiveScoreMapper, StudentComprehensiveScore>
        implements IStudentComprehensiveScoreService {

    @Autowired
    private StudentComprehensiveScoreMapper studentComprehensiveScoreMapper;

    /**
     * 检查学生是否已报名该活动
     *
     * @param studentId 学号
     * @param activityName 活动名称
     * @return true 已报名且状态为“通过”，false 未报名或未通过
     */
    @Override
    public boolean isActivitySignedUp(String studentId, String activityName) {
        return studentComprehensiveScoreMapper.existsByStudentAndActivity(studentId, activityName);
    }

    /**
     * 学生报名活动
     *
     * @param score 活动信息
     * @return 是否成功
     */
    @Override
    public boolean signUpActivity(StudentComprehensiveScore score) {
        // 检查是否已报名
        if (isActivitySignedUp(score.getStudentId(), score.getActivityName())) {
            return false;
        }

        // 如果 participationTime 为 null，则设为当前时间
        if (score.getParticipationTime() == null) {
            score.setParticipationTime(new Date());
        }

        // 插入新记录
        return this.save(score);
    }
}
